package ar.edu.utn.frba.dds.opendesigners;

import com.lanchita.AerolineaLanchita;

import java.math.BigDecimal;

public class LanchitaAdapter implements AirlinesAdapter {

	@Override
	public SearchResponse search(SearchParam paramSearch) {
		
		SearchResponse response = new SearchResponse();
		
		AerolineaLanchita lanchita = AerolineaLanchita.getInstance();
		String[][] resultados = lanchita.asientosDisponibles(
				paramSearch.getOriginName(), paramSearch.getDestinationName(), 
				paramSearch.getOriginDate(), paramSearch.getOriginHour(), 
				paramSearch.getDestinationDate(), paramSearch.getDestinationHour());
		
		for (int i = 0; i < resultados.length; i++)
		{
			RouteOption route = new RouteOption();
			
			String[] arrResult = resultados[i];
			route.setAirline("Aerolinea");
			route.setFlightNumber(arrResult[0].split("-")[0]);
			route.setSeatNumber(arrResult[0].toString().split("-")[1]);
			
			BigDecimal price = new BigDecimal(new Double(arrResult[1]));
			
			//Agrego el recargo que aplica la aerolinea
			price = price.multiply(this.getSurcharge());
			
			route.setPrice(price);
			route.setSeatType(arrResult[2].toCharArray()[0]);
			route.setSeatLocation(arrResult[3].toCharArray()[0]);
			
			if (arrResult[4] == "P")
				route.setState(true);
			else
				route.setState(false);
			
			response.add(route);
		}
		return response;
	}

	@Override
	public int buy(RouteOption option) {
		AerolineaLanchita lanchita = AerolineaLanchita.getInstance();
		
		String codigo = option.getFlightNumber() + "-" + option.getSeatNumber();
		lanchita.comprar(codigo);
		
		return 0;
	}

	@Override
	public String getAirline() {
		return "Lanchita";
	}

	@Override
	public BigDecimal getSurcharge() {
		return new java.math.BigDecimal("1.15");
	}

}
